package leetcode.code0454;

import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
		Map<Integer, Integer> hash = new HashMap<>();
		int n = nums1.length;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				int sum = nums1[i] + nums2[j];
				int count = hash.getOrDefault(sum, 0);
				hash.put(sum, count + 1);
			}
		}
		int ans = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				int sum = nums3[i] + nums4[j];
				int count = hash.getOrDefault(-sum, 0);
				ans += count;
			}
		}
		return ans;
	}

//	public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
//		Map<Integer, List<int[]>> hash = new HashMap<>();
//		int n = nums1.length;
//		for (int i = 0; i < n; i++) {
//			for (int j = 0; j < n; j++) {
//				int sum = nums1[i] + nums2[j];
//				List<int[]> list = hash.getOrDefault(sum, new ArrayList<>());
//				list.add(new int[] { i, j });
//				hash.put(sum, list);
//			}
//		}
//		int ans = 0;
//		for (int i = 0; i < n; i++) {
//			for (int j = 0; j < n; j++) {
//				int sum = nums3[i] + nums4[j];
//				List<int[]> list = hash.getOrDefault(sum, new ArrayList<>());
//				list.add(new int[] { i, j });
//				hash.put(sum, list);
//			}
//		}
//		return ans;
//	}

	@Override
	public void debug1() {
		HelpDebug.compare(2, this.fourSumCount(HelpDebug.str2array3("[1,2]"), HelpDebug.str2array3("[-2,-1]"), HelpDebug.str2array3("[-1,2]"),
				HelpDebug.str2array3("[0,2]")));
	}

	@Override
	public void debug2() {
		HelpDebug.compare(1,
				this.fourSumCount(HelpDebug.str2array3("[0]"), HelpDebug.str2array3("[0]"), HelpDebug.str2array3("[0]"), HelpDebug.str2array3("[0]")));
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
